package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

func main() {
	fmt.Println(" >>> ")
	fmt.Println("--------------------------------")
	path := "./"
	file, _ := os.Stat("./go1.4")
	fmt.Println(path)
	fmt.Println(file.Name())
	dfs(file, path)
}

func dfs(d os.FileInfo, path string) {
	if !d.IsDir() {
		filename := path + "/" + d.Name()
		readFile(filename)
		return
	}
	path += "/" + d.Name()
	newpath := strings.Replace(path, "./", "../", 1)
	err := os.MkdirAll(newpath, 0777)
	checkError(err)
	fmt.Println(path, " >> ", newpath)
	dirs, _ := ioutil.ReadDir(path)
	for _, dir := range dirs {
		dfs(dir, path)
	}
}

func readFile(filename string) {
	file, _ := os.Open(filename)
	defer file.Close()
	name := file.Name()
	newname := strings.Replace(name, "./", "../", 1)
	fmt.Println(name, "  >>> ", newname)
	dst, err := os.Create(newname + ".py")
	if checkError(err) {
		return
	}
	writer := bufio.NewWriter(dst)
	io.Copy(writer, file)
	writer.Flush()
	defer dst.Close()
}

func checkError(err error) bool {
	if err != nil {
		fmt.Println(err.Error())
		return true
	}
	return false
}
